<template>
  <div class="stage-manage">
    <!-- 顶部操作区 -->
    <div class="operation-bar">
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>添加语言
      </el-button>
    </div>

    <!-- 表格区域 -->
    <el-table 
      :data="tableData.slice((currentPage-1)*pageSize, currentPage*pageSize)" 
      style="width: 100%"
      :height="600"
      border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="langName" label="语言名称" width="150" />
      <el-table-column prop="description" label="语言描述" show-overflow-tooltip />
      <el-table-column prop="createTime" label="添加时间" width="180" />
      <el-table-column prop="creator" label="创建者" width="120" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.status ? 'success' : 'info'">
            {{ scope.row.status ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="scope">
          <el-button 
            type="primary" 
            :icon="Edit"
            circle
            @click="handleEdit(scope.row)" />
          <el-button 
            type="danger" 
            :icon="Delete"
            circle
            @click="handleDelete(scope.row)" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '添加语言' : '编辑语言'"
      width="500px"
    >
      <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
        <el-form-item label="语言名称" prop="langName">
          <el-input v-model="form.langName" placeholder="请输入语言名称"/>
        </el-form-item>
        <el-form-item label="语言描述" prop="description">
          <el-input 
            v-model="form.description" 
            type="textarea" 
            :rows="3"
            placeholder="请输入语言描述"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit(formRef)">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { Edit, Delete, Plus } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';

// 表格高度
// 表单校验规则
const rules = {
  stageName: [
    { required: true, message: '请输入语言名称', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  description: [
    { required: true, message: '请输入语言描述', trigger: 'blur' }
  ]
};

// 分页相关
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);

// 对话框相关
const dialogVisible = ref(false);
const dialogType = ref('add');
const formRef = ref(null);
const form = ref({
  stageName: '',
  description: '',
  status: true
});

// 模拟数据
const tableData = ref([
  {
    id: 1,
    langName: 'Python',
    description: 'Python在各个编程语言中比较适合新手学习，Python解释器易于扩展',
    createTime: '2024-01-01 10:00:00',
    creator: '管理员',
    status: true
  },
  {
    id: 2,
    langName: 'Java',
    description: 'Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。',
    createTime: '2024-01-02 14:30:00',
    creator: '管理员',
    status: true
  }
]);

// 计算总数
total.value = tableData.value.length;

// 添加阶段
const handleAdd = () => {
  dialogType.value = 'add';
  form.value = {
    langName: '',
    description: '',
    status: true
  };
  dialogVisible.value = true;
};

// 编辑阶段
const handleEdit = (row) => {
  dialogType.value = 'edit';
  form.value = { ...row };
  dialogVisible.value = true;
};

// 删除阶段
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确定要删除该语言吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      // 这里添加删除逻辑
      ElMessage({
        type: 'success',
        message: '删除成功',
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消删除',
      });
    });
};

// 提交表单
const handleSubmit = async (formEl) => {
  if (!formEl) return;
  
  await formEl.validate((valid) => {
    if (valid) {
      if (dialogType.value === 'add') {
        // 添加逻辑
        const newStage = {
          id: tableData.value.length + 1,
          ...form.value,
          createTime: new Date().toLocaleString(),
          creator: '当前用户'
        };
        tableData.value.push(newStage);
        ElMessage.success('添加成功');
      } else {
        // 编辑逻辑
        const index = tableData.value.findIndex(item => item.id === form.value.id);
        if (index !== -1) {
          tableData.value[index] = { ...form.value };
        }
        ElMessage.success('修改成功');
      }
      dialogVisible.value = false;
    }
  });
};


</script>

<style scoped>
.stage-manage {
  padding: 20px;
}

.operation-bar {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-button {
  margin-right: 10px;
}

:deep(.el-table) {
  margin-top: 20px;
}

:deep(.el-dialog__body) {
  padding: 20px;
}
</style>
